import { createRouter, createWebHistory } from 'vue-router'
import Login from '@/views/Login/index.vue'
import Layout from '@/views/Layout/index.vue'
import Home from '@/views/Home/index.vue'
import Category from '@/views/Category/index.vue'
import SubCategory from '@/views/SubCategory/index.vue'
import Detail from '@/views/Detail/index.vue'
import CartList from '@/views/CartList/index.vue'
import Checkout from '@/views/Checkout/index.vue'
import Pay from '@/views/Pay/index.vue'
import PayBack from "@/views/Pay/PayBack.vue";
import Member from '@/views/Member/index.vue'
import UserInfo from '@/views/Member/components/UserInfo.vue'
import UserOrder from '@/views/Member/components/UserOrder.vue'


// createRouter:创建router实例对象
// createWebHistory:创建history模式的路由

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: '/',
            name: 'layout',
            component: Layout,
            // 添加二级路由
            children: [
                {
                    path: '', // 置空, 当访问 / 时 , home 组件显示
                    name: 'home',
                    component: Home,
                },
                {
                    path: '/category/:id', // 占位id参数
                    name: 'category',
                    component: Category,
                },
                {
                    path: '/category/sub/:id', // 占位id参数
                    name: 'subCategory',
                    component: SubCategory,
                },
                {
                    path: '/detail/:id',
                    name: 'detail',
                    component: Detail,
                },
                {
                    path: '/cartlist',
                    name: 'cartlist',
                    component: CartList,
                },
                {
                    path: '/checkout',
                    name: 'checkout',
                    component: Checkout,
                },
                {
                    path: '/pay',
                    name: 'pay',
                    component: Pay
                },
                {
                    path: '/paycallback', // 注意路径，必须是paycallback
                    name: 'paycallback',
                    component: PayBack
                },
                {
                    path: '/member',
                    name: 'member',
                    component: Member,
                    children: [
                        {
                            path: '',
                            name: 'userInfo',
                            component: UserInfo
                        },
                        {
                            path: 'order',
                            name: 'userOrder',
                            component: UserOrder
                        }
                    ]
                }
            ]
        },
        {
            path: '/login',
            name: 'login',
            component: Login
        },
    ],
    /* 配置这个路由行为，就可以实现点击一级路由时，自动跳转到浏览器最顶部 */
    scrollBehavior() {
        return {top: 0}
    }
})

export default router
